home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / bzr_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-29  |  10.2 KB  |  248 lines

  1. /******************************************************************************
  2. * Bzr_Wrt.c - Bezier handling routines - write to file.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "prsr_loc.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. * Writes Bezier curve(s) list into file. Returns TRUE if succesful, FALSE    M
  15. * otherwise.                                     M
  16. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  17. * is written.                                     M
  18. *                                                                            *
  19. * PARAMETERS:                                                                M
  20. *   Crvs:       To write to file FileName.                                   M
  21. *   FileName:   Name of file to open so we can write Crvs in.                M
  22. *   Indent:     Primary indentation. All information will be written         M
  23. *               from the column specified by Indent.                         M
  24. *   Comment:    Optional, to describe the geometry.                          M
  25. *   ErrStr:     If an error occurs, to describe the error.                   M
  26. *                                                                            *
  27. * RETURN VALUE:                                                              M
  28. *   int:        TRUE if succesful, FALSE otherwise.                          M
  29. *                                                                            *
  30. * KEYWORDS:                                                                  M
  31. *   BzrCrvWriteToFile, files, write                                          M
  32. *****************************************************************************/
  33. int BzrCrvWriteToFile(CagdCrvStruct *Crvs,
  34.               char *FileName,
  35.               int Indent,
  36.               char *Comment,
  37.               char **ErrStr)
  38. {
  39.     int i, Handler;
  40.     FILE *f;
  41.  
  42.     if ((f = fopen(FileName, "w")) == NULL) {
  43.     *ErrStr = "Fail to open file";
  44.     return FALSE;
  45.     }
  46.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  47.                      IritPrsrSenseBinaryFile(FileName),
  48.                      FALSE);
  49.  
  50.     i = BzrCrvWriteToFile2(Crvs, Handler, Indent, Comment, ErrStr);
  51.  
  52.     IritPrsrCloseStream(Handler, TRUE);
  53.  
  54.     return i;
  55. }
  56.  
  57. /*****************************************************************************
  58. * DESCRIPTION:                                                               M
  59. * Writes Bezier curve(s) list into file. Returns TRUE if succesful, FALSE    M
  60. * otherwise. The file descriptor is not closed.                     M
  61. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  62. * is written.                                     M
  63. *                                                                            *
  64. * PARAMETERS:                                                                M
  65. *   Crvs:       To write to open stream.                                     M
  66. *   Handler:    A handler to the open stream.                     M
  67. *   Indent:     Primary indentation. All information will be written         M
  68. *               from the column specified by Indent.                         M
  69. *   Comment:    Optional, to describe the geometry.                          M
  70. *   ErrStr:     If an error occurs, to describe the error.                   M
  71. *                                                                            *
  72. * RETURN VALUE:                                                              M
  73. *   int:        TRUE if succesful, FALSE otherwise.                          M
  74. *                                                                            *
  75. * KEYWORDS:                                                                  M
  76. *   BzrCrvWriteToFile2, files, write                                         M
  77. *****************************************************************************/
  78. int BzrCrvWriteToFile2(CagdCrvStruct *Crvs,
  79.                int Handler,
  80.                int Indent,
  81.                char *Comment,
  82.                char **ErrStr)
  83. {
  84.     int i, j, MaxCoord;
  85.  
  86.     if (Comment != NULL) {
  87.     _IPFprintf(Handler, Indent, "#\n");
  88.     _IPFprintf(Handler, Indent, "# cagd_lib - bezier curve(s) dump.\n");
  89.     _IPFprintf(Handler, Indent, "#\n");
  90.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  91.     _IPFprintf(Handler, Indent, "#\n");
  92.     }
  93.  
  94.     *ErrStr = NULL;
  95.  
  96.     while (Crvs) {
  97.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  98.  
  99.     if (Crvs -> GType != CAGD_CBEZIER_TYPE) {
  100.         *ErrStr = "Given curve(s) is (are) not BEZIER curve(s)";
  101.         break;
  102.     }
  103.     _IPFprintf(Handler, Indent, "[CURVE BEZIER %d %c%c\n",
  104.         Crvs -> Length,
  105.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  106.         MaxCoord + '0');
  107.     Indent += 4;
  108.  
  109.     for (i = 0; i < Crvs -> Length; i++) {
  110.         _IPFprintf(Handler, Indent, "[");
  111.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  112.         _IPFprintf(Handler, 0, "%s ", _IPReal2Str(Crvs -> Points[0][i]));
  113.         for (j = 1; j <= MaxCoord; j++) {
  114.         _IPFprintf(Handler, 0, "%s", _IPReal2Str(Crvs -> Points[j][i]));
  115.         if (j < MaxCoord) _IPFprintf(Handler, 0, " ");
  116.         }
  117.         _IPFprintf(Handler, 0, "]\n");
  118.     }
  119.  
  120.     Indent -= 4;
  121.     _IPFprintf(Handler, Indent, "]\n");
  122.  
  123.     Crvs = Crvs -> Pnext;
  124.     }
  125.  
  126.     return *ErrStr == NULL;
  127. }
  128.  
  129. /*****************************************************************************
  130. * DESCRIPTION:                                                               M
  131. * Writes Bezier surface(s) list into file. Returns TRUE if succesful, FALSE  M
  132. * otherwise.                                     M
  133. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  134. * is written.                                     M
  135. *                                                                            *
  136. * PARAMETERS:                                                                M
  137. *   Srfs:       To write to file FileName.                                   M
  138. *   FileName:   Name of file to open so we can write Srfs in.                M
  139. *   Indent:     Primary indentation. All information will be written         M
  140. *               from the column specified by Indent.                         M
  141. *   Comment:    Optional, to describe the geometry.                          M
  142. *   ErrStr:     If an error occurs, to describe the error.                   M
  143. *                                                                            *
  144. * RETURN VALUE:                                                              M
  145. *   int:        TRUE if succesful, FALSE otherwise.                          M
  146. *                                                                            *
  147. * KEYWORDS:                                                                  M
  148. *   BzrSrfWriteToFile, files, write                                          M
  149. *****************************************************************************/
  150. int BzrSrfWriteToFile(CagdSrfStruct *Srfs,
  151.               char *FileName,
  152.               int Indent,
  153.               char *Comment,
  154.               char **ErrStr)
  155. {
  156.     int i, Handler;
  157.     FILE *f;
  158.  
  159.     if ((f = fopen(FileName, "w")) == NULL) {
  160.     *ErrStr = "Fail to open file";
  161.     return FALSE;
  162.     }
  163.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  164.                      IritPrsrSenseBinaryFile(FileName),
  165.                      FALSE);
  166.  
  167.     i = BzrSrfWriteToFile2(Srfs, Handler, Indent, Comment, ErrStr);
  168.  
  169.     IritPrsrCloseStream(Handler, TRUE);
  170.  
  171.     return i;
  172. }
  173.  
  174. /*****************************************************************************
  175. * DESCRIPTION:                                                               M
  176. * Writes Bezier surface(s) list into file. Returns TRUE if succesful, FALSE  M
  177. * otherwise. The file descriptor is not closed.                     M
  178. *   If Comment is NULL, no comment is wrriten, if "" only internal comment   M
  179. * is written.                                     M
  180. *                                                                            *
  181. * PARAMETERS:                                                                M
  182. *   Srfs:       To write to open stream.                                     M
  183. *   Handler:    A handler to the open stream.                     M
  184. *   Indent:     Primary indentation. All information will be written         M
  185. *               from the column specified by Indent.                         M
  186. *   Comment:    Optional, to describe the geometry.                          M
  187. *   ErrStr:     If an error occurs, to describe the error.                   M
  188. *                                                                            *
  189. * RETURN VALUE:                                                              M
  190. *   int:        TRUE if succesful, FALSE otherwise.                          M
  191. *                                                                            *
  192. * KEYWORDS:                                                                  M
  193. *   BzrSrfWriteToFile2, files, write                                         M
  194. *****************************************************************************/
  195. int BzrSrfWriteToFile2(CagdSrfStruct *Srfs, 
  196.                int Handler,
  197.                int Indent,
  198.                char *Comment,
  199.                char **ErrStr)
  200. {
  201.     int i, j, MaxCoord;
  202.  
  203.     if (Comment != NULL) {
  204.     _IPFprintf(Handler, Indent, "#\n");
  205.     _IPFprintf(Handler, Indent, "# cagd_lib - bezier srf(s) dump.\n");
  206.     _IPFprintf(Handler, Indent, "#\n");
  207.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  208.     _IPFprintf(Handler, Indent, "#\n");
  209.     }
  210.  
  211.     *ErrStr = NULL;
  212.  
  213.     while (Srfs) {
  214.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  215.  
  216.     if (Srfs -> GType != CAGD_SBEZIER_TYPE) {
  217.         *ErrStr = "Given surface(s) is (are) not BEZIER surface(s)";
  218.         break;
  219.     }
  220.     _IPFprintf(Handler, Indent, "[SURFACE BEZIER %d %d %c%c\n",
  221.         Srfs -> ULength, Srfs -> VLength,
  222.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  223.         MaxCoord + '0');
  224.     Indent += 4;
  225.  
  226.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  227.         if (i && i % Srfs -> ULength == 0)
  228.         _IPFprintf(Handler, 0, "\n");    /* Put empty lines between raws. */
  229.  
  230.         _IPFprintf(Handler, Indent, "[");
  231.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  232.         _IPFprintf(Handler, 0, "%s ", _IPReal2Str(Srfs -> Points[0][i]));
  233.         for (j = 1; j <= MaxCoord; j++) {
  234.         _IPFprintf(Handler, 0, "%s", _IPReal2Str(Srfs -> Points[j][i]));
  235.         if (j < MaxCoord) _IPFprintf(Handler, 0, " ");
  236.         }
  237.         _IPFprintf(Handler, 0, "]\n");
  238.     }
  239.  
  240.     Indent -= 4;
  241.     _IPFprintf(Handler, Indent, "]\n");
  242.  
  243.     Srfs = Srfs -> Pnext;
  244.     }
  245.  
  246.     return *ErrStr == NULL;
  247. }
  248.